package cn.edu.neusoft.amicooldemo;

import android.content.Intent;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.amicooldemo.bean.Constants;
import cn.edu.neusoft.amicooldemo.bean.LoginBean;
import cn.edu.neusoft.amicooldemo.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RegisterActivity extends AppCompatActivity implements View.OnFocusChangeListener, View.OnClickListener {

    private static final String TAG="RegisterActivity";

    private EditText username;
    private EditText password;
    private EditText confirmPassword;
    private EditText realName;
    private EditText age;
    private EditText phone;

    private ImageView backBtn;
    private TextView regBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        init();
        initEvent();
    }

    private void initEvent() {
        //editView焦点是无提示
        username.setOnFocusChangeListener(this);
        password.setOnFocusChangeListener(this);
        confirmPassword.setOnFocusChangeListener(this);
        realName.setOnFocusChangeListener(this);
        age.setOnFocusChangeListener(this);
        phone.setOnFocusChangeListener(this);

        backBtn.setOnClickListener(this);
        regBtn.setOnClickListener(this);
    }

    private void init() {
        username = (EditText) findViewById(R.id.reg_username);
        password = (EditText) findViewById(R.id.reg_password);
        confirmPassword = (EditText) findViewById(R.id.reg_confirm_password);
        realName = (EditText) findViewById(R.id.reg_realname);
        age = (EditText) findViewById(R.id.reg_age);
        phone = (EditText) findViewById(R.id.reg_phone);

        backBtn = (ImageView) findViewById(R.id.reg_back);
        regBtn = (TextView) findViewById(R.id.reg_regBtn);
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        EditText _v = (EditText)v;
        if (!hasFocus){
            _v.setHint(_v.getTag().toString());
        } else {
            String hint = _v.getHint().toString();
            _v.setTag(hint);
            _v.setHint("");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reg_back:
                startActivity(new Intent(this,LoginActivity.class));
                finish();
                break;
            case R.id.reg_regBtn:
                register();
        }
    }

    private void register() {
        //用户名密码，真是姓名不能为空
        if("".equals(username.getText().toString())){
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        } else if("".equals(password.getText().toString())){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
        } else if(password.getText().toString().length()<6){
            Toast.makeText(this, "密码长度应6位以上", Toast.LENGTH_SHORT).show();
        }else if(!password.getText().toString().equals(confirmPassword.getText().toString())){
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        } else if(phone.getText().toString().length()!=11 ){
            Toast.makeText(this, "手机输入错误", Toast.LENGTH_SHORT).show();
        }else{
            Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.SERVERADDRESS).addConverterFactory(ScalarsConverterFactory.create()).build();
            UserService service  = retrofit.create(UserService.class);
            Call<String> call = service.register(username.getText().toString(),password.getText().toString(),phone.getText().toString());
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    String msg = response.body().toString();
                    if (msg.equals("1")){
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        finish();
                    }
                    else{
                        Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                    }
                    Log.d(TAG, "注册信息反馈: "+response.body().toString());
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Toast.makeText(RegisterActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onFailure: "+t.toString());
                }
            });
        }
    }
}
